//===- AADriver.cpp - Driver for Alias Analysis" --------------------------===//
//
//	Copyright (C) 2014	Victor Hugo Sperle Campos
//						George Souza Oliveira
//				
//
//===----------------------------------------------------------------------===//


#include "AADriver.h"

using namespace llvm;


bool AADriver::runOnModule(Module &M) {

	// Fetch Alias Analysis information
	this->AA = &getAnalysis<AliasAnalysis>();
	
	// Build Alias sets
	this->AST = new AliasSetTracker(*this->AA);
	
	// As of now, creating alias sets for all instructions in program
	for (Module::iterator Mit = M.begin(), Mend = M.end(); Mit != Mend; ++Mit) {
		Function& F = *Mit;
		
		for (Function::iterator Fit = F.begin(), Fend = F.end(); Fit != Fend; ++Fit) {
			BasicBlock& BB = *Fit;
			
			// Adds all instructions of BB to the alias set tracker
			for (BasicBlock::iterator BBit = BB.begin(), BBend = BB.end(); BBit != BBend; ++BBit) {
				Instruction& I = *BBit;

				if (isa<StoreInst>(I)) {
					this->AST->add(&I);
				}
			}
		}
	}
	
	// Print alias sets
	for (AliasSetTracker::const_iterator ASTit = this->AST->begin(), ASTend = this->AST->end(); ASTit != ASTend; ++ASTit) {
		const AliasSet& AS = *ASTit;
		
		AS.print(errs());
	}
	
	delete this->AST;
	
	return false;
}

void AADriver::getAnalysisUsage(AnalysisUsage &AU) const {
	AU.addRequired<AliasAnalysis>();
 	AU.setPreservesAll();
}
  

char AADriver::ID = 0;
static RegisterPass<AADriver> Y("aadriver", "AADriver World Pass (with getAnalysisUsage implemented)", false, false);
